package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.po.cache.CachePo;
import com.bwda.dsrs.filecenter.domain.condition.cache.CacheCondition;
import com.bwda.dsrs.filecenter.domain.form.cache.CacheQueryForm;
import com.bwda.dsrs.filecenter.domain.form.cache.CacheUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.cache.CacheCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.cache.CacheVo;
import com.bwda.dsrs.filecenter.service.CacheService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;

@RestController
@RequestMapping("/cache")
@Api(value = "/cache", description = "token缓存表")
public class CacheController extends BaseController {

	@Autowired
	private CacheService cacheService;

	@ApiOperation(value = "查询token缓存表",notes = "查询token缓存表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CacheVo> query(@ApiParam(value = "ID", required = true)@RequestParam Integer id) throws BusinessServiceException {
		CachePo po = cacheService.queryWithValid(id);
		CacheVo vo = CopyUtil.transfer(po, CacheVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询token缓存表数量",notes = "查询token缓存表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid CacheQueryForm form) throws BusinessServiceException {
		CacheCondition condition = this.getConditionByQueryForm(form);
		int count = cacheService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询token缓存表列表",notes = "查询token缓存表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<CacheVo>> queryList(@ModelAttribute@Valid CacheQueryForm form) throws BusinessServiceException {
		CacheCondition condition = this.getConditionByQueryForm(form);
		List<CachePo> poList = cacheService.queryList(condition);
		List<CacheVo> voList = CopyUtil.transfer(poList, CacheVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询token缓存表列表(带分页)",notes = "查询token缓存表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<CacheVo>> queryPageList(@ModelAttribute@Valid CacheQueryForm form) throws BusinessServiceException {
		CacheCondition condition = this.getConditionByQueryForm(form);
		List<CacheVo> voList = new ArrayList<>();
		int count = cacheService.queryCount(condition);
		if (count > 0) {
			List<CachePo> poList = cacheService.queryList(condition);
			voList = CopyUtil.transfer(poList, CacheVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增token缓存表",notes = "新增token缓存表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CacheVo> add(@ModelAttribute@Valid CacheCreateForm form) throws BusinessServiceException {
		CachePo po = CopyUtil.transfer(form, CachePo.class);
		po.setId(RandomUtils.nextInt());
		cacheService.insert(po);
		CacheVo vo = CopyUtil.transfer(po, CacheVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改token缓存表",notes = "修改token缓存表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid CacheUpdateForm form) throws BusinessServiceException {
		CachePo po = CopyUtil.transfer(form, CachePo.class);
		cacheService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除token缓存表",notes = "删除token缓存表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "ID", required = true)@RequestParam Integer id) throws BusinessServiceException {
		cacheService.delete(id);
		return getSuccessResult();
	}

	/**
	 * CacheQueryForm转换为CacheCondition
	 *
	 * @param form
	 * @return
	 */
	private CacheCondition getConditionByQueryForm(CacheQueryForm form) {
		CacheCondition condition = CopyUtil.transfer(form, CacheCondition.class);
		return condition;
	}

}